/////////////////////////////////////////////////////////////////////////////
/*/	
 *	Creator:		Joshua C. Rouzer
 *	Title:			Shielder cpp
 *	Last Edited:	06/14/2013
 *	Summary:		Behavioral Initialization of the Shielder unit
/*/
/////////////////////////////////////////////////////////////////////////////
#include "Shielder.h"
#include "LevelManager.h"
#include "DefenseStruct.h"
#include "SGD_Math.h"
#include "CSGD_TextureManager.h"
#include <queue>
/////////////////////////////////////////////////////////////////////////////
// Function : Default Constructor
// Notes	: Gives the basic traits to the unit
/////////////////////////////////////////////////////////////////////////////
CShielder::CShielder(){
	m_eType = SHIELD;
	SetHealth(100);
	SetMaxHealth(100);
	SetAttackCool(1);
	SetSpecAttCool(20);
	SetBaseDmg(10);
	SetSpecialDmg(20);
	SetAttackWait(1);
	SetSAttackWait(20);
	SetImage( CSGD_TextureManager::GetInstance()->LoadTexture( _T("resource/Images/Shielder.png"), D3DCOLOR_ARGB(255,255,255,255) ) );
	SetWidth(64);
	SetHeight(64);
}
/////////////////////////////////////////////////////////////////////////////
// Function		: Destructor
// Notes		: Cleans up any dynamically allocated memory
/////////////////////////////////////////////////////////////////////////////
CShielder::~CShielder(){}
/////////////////////////////////////////////////////////////////////////////
// Function		: Update
// Parameters	: dT - Time elapsed since last update
// Notes		: Handles movement and behavior of the unit
/////////////////////////////////////////////////////////////////////////////
void CShielder::Update(float dT){

	priority_queue<CBaseUnit*> enemyQ;
	if(GetTarget() == NULL)
	{

		std::vector<CBaseUnit*> swapVec;

		if(this->GetTeam() == 1)
			swapVec = CLevelManager::GetInstance()->GetCurrLevel()->GetTeam2()->GetPath2Units();
		else
			swapVec = CLevelManager::GetInstance()->GetCurrLevel()->GetTeam1()->GetPath2Units();

			for(unsigned int i = 0; i < swapVec.size();i++)
				enemyQ.push(swapVec[i]);
						for(unsigned int qIter = 0; qIter < enemyQ.size(); )
			{
				CBaseUnit *_enemy = enemyQ.top();

				if(CheckCollision(_enemy) == true)
				{
				//YES
					//Break loop
					//Attack it
					SetTarget(_enemy);
					break;
				}
				else
				{
					//NO
					//Access next target in queue
					enemyQ.pop();
					//Restart loop
				}
			}
	
	}

	if(GetTarget() == nullptr){
		//Walk to the next waypoint
		tVector2D toWaypoint;
		toWaypoint.fX = GetWaypoint()->GetPos().m_nPosX - GetPosX();
		toWaypoint.fY = GetWaypoint()->GetPos().m_nPosY - GetPosY();
		toWaypoint = Vector2DNormalize( toWaypoint );

		toWaypoint.fX = toWaypoint.fX * GetVelX() * dT;
		toWaypoint.fY = toWaypoint.fY * GetVelY() * dT;

		SetPosX( GetPosX() + toWaypoint.fX );
		SetPosY( GetPosY() + toWaypoint.fY );

	}
}
/////////////////////////////////////////////////////////////////////////////
// Function		: Render
// Notes		: Renders the unit to the screen
/////////////////////////////////////////////////////////////////////////////
void CShielder::Render(){
	CBaseUnit::Render();
}

std::vector<CBaseUnit*> CShielder::GetEnemiesInRange()
{
	std::vector<CBaseUnit*> fullList;
	std::vector<CBaseUnit*> returnList;

	if(this->GetTeam() == 1)
	{
		if(this->GetPath() == 1)
			fullList = CLevelManager::GetInstance()->GetCurrLevel()->GetTeam2()->GetPath1Units();
		else if(this->GetPath() == 2)
			fullList = CLevelManager::GetInstance()->GetCurrLevel()->GetTeam2()->GetPath2Units();
		else if(this->GetPath() == 3)
			fullList = CLevelManager::GetInstance()->GetCurrLevel()->GetTeam2()->GetPath3Units();
	}
	else if(this->GetTeam() == 2)
	{
		if(this->GetPath() == 1)
			fullList = CLevelManager::GetInstance()->GetCurrLevel()->GetTeam1()->GetPath1Units();
		else if(this->GetPath() == 2)
			fullList = CLevelManager::GetInstance()->GetCurrLevel()->GetTeam1()->GetPath2Units();
		else if(this->GetPath() == 3)
			fullList = CLevelManager::GetInstance()->GetCurrLevel()->GetTeam1()->GetPath3Units();
	}

	for (unsigned int i = 0; i < fullList.size(); i++)
	{
		SPosition onePos;
		onePos.m_nPosX = (fullList[i]->GetPosX());
		onePos.m_nPosY = (fullList[i]->GetPosY());
		SPosition twoPos;
		twoPos.m_nPosX = (this->GetPosX());
		twoPos.m_nPosY = (this->GetPosY());
#if _DEBUG
		int test = DistanceBetween(onePos,twoPos);
#endif
		if(DistanceBetween(onePos,twoPos) < GetRange())
			returnList.push_back(fullList[i]);
	}

	return returnList;
}
